package com.itheima.service.impl;

import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.service.RoleAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.mapper.RoleAdminMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.apache.dubbo.config.annotation.Service;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class RoleAdminServiceImpl implements RoleAdminService {

    @Autowired
    private RoleAdminMapper roleAdminMapper;

    @Override
    public List<Role> findRoleAll() {
        return roleAdminMapper.findRoleAll();
    }

    @Override
    public Map findRoleIdsByUserId(Integer id) {
        Map map = new HashMap();
        List<Role> list = roleAdminMapper.findRoleAll();
        Integer[] roleIds = roleAdminMapper.findRoleIdsByUserId(id);
        map.put("list",list);
        map.put("roleIds",roleIds);
        return map;
    }

    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        Map map = new HashMap();
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Role> list = roleAdminMapper.findPage(queryPageBean.getQueryString());
        if (list != null && list.size() > 0) {
            for (Role role : list) {
                Integer id = role.getId();
                List<Integer> permissionIds = roleAdminMapper.findRoleAndPermissionById(id);
                if(permissionIds!=null&&permissionIds.size()>0){
                    for (Integer permissionId : permissionIds) {
                        String name = roleAdminMapper.findPermissionName(permissionId);
                        Set<Permission> permissions = new HashSet<>();
                        Permission permission = new Permission();
                        permission.setName(name);
                        permissions.add(permission);
                        role.setPermissions(permissions);
                    }
                }
            }
        }
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public void add(Role role, Integer[] permissionIds) {
        roleAdminMapper.add(role);
        Integer roleId = role.getId();

        if(permissionIds!=null){
            for (Integer permissionId : permissionIds) {
                roleAdminMapper.addRoleAndPermission(roleId,permissionId);
            }
        }
    }

    @Override
    public void deleteRoleAdminById(Integer id) {
        roleAdminMapper.removeRoleAndPermissionByRoleId(id);
        roleAdminMapper.deleteRoleAdminById(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds) {
        roleAdminMapper.edit(role);

        Integer roleId = role.getId();

        roleAdminMapper.removeRoleAndPermissionByRoleId(roleId);

        if(permissionIds!=null){
            for (Integer permissionId : permissionIds) {
                roleAdminMapper.addRoleAndPermission(roleId,permissionId);
            }
        }
    }


}
